
package security.beans;

import Entity.Profile;
import Entity.TableModelProfile;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import login.beans.ConectionBean;
import security.business.ProfileBusiness;

/**
 *
 * @author JuanFelipe
 */
@ManagedBean (name = "profileBean")
@RequestScoped
public class ProfileBean implements Serializable{
   
    private List<Profile>profiles;
    private Profile selectedProfile;
    private TableModelProfile tableModel;
    
    public ProfileBean() {
        FacesContext context = javax.faces.context.FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        ConectionBean conectionBean =(ConectionBean) session.getAttribute("conectionBean");
        
        selectedProfile = new Profile("", "", "", "", "");
        init( conectionBean.currentConnection );
    }

    private void init(Connection connection) {
        try {
            profiles = new ProfileBusiness( connection ).getProfile( );
            tableModel = new TableModelProfile( profiles );
        } catch (SQLException ex) {
            FacesContext.getCurrentInstance().addMessage(
                    null,
                    new FacesMessage(FacesMessage.SEVERITY_WARN,
                    "Please Try Again!",
                    ex.getMessage()));
        }
    }

    public List<Profile> getProfiles() {
        return profiles;
    }

    public void setProfiles(List<Profile> profiles) {
        this.profiles = profiles;
    }

    public Profile getSelectedProfile() {
        return selectedProfile;
    }

    public void setSelectedProfile(Profile selectedProfile) {
        this.selectedProfile = selectedProfile;
    }

    public TableModelProfile getTableModel() {
        return tableModel;
    }

    public void setTableModel(TableModelProfile tableModel) {
        this.tableModel = tableModel;
    }
    
    public void delete(){
        throw new UnsupportedOperationException("Not supported yet."); 
    }
}
